/**
 * 角色管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：角色管理页面
     创建日期：2015-11-17
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	roleList();
});

/**
 * 查询资源列表
 * @returns
 */
var roleList = function(){
	
	var datas = {"p":"roleManage","m":"listPaged" };//请求参数
	var columnDefine = {"id":"ID","name":"名称"};//表格栏目定义
	var formatters = {};//自定义表格格式
	var buttons =''; //操作按钮
	buttons+='<p>';
	buttons+='<button type=\'button\' class=\'btn btn-w-m btn-info\' onclick="showModifyWin();">编辑</button>&nbsp;&nbsp;';
	buttons+='<button type=\'button\' class=\'btn btn-w-m btn-danger\' onclick="openSetupWin();">设置权限</button>';
	buttons+='</p>';
	hui.pager('service',datas ,"dataList", columnDefine , formatters , buttons ); //分页查询
};



/**
 * 打开新建窗口
 * @returns
 */
var showNewWin = function(){
	$('#newPop input').val('');
	$('#newPop').modal('show');
};


/**
 * 打开设置权限窗口
 * @returns
 */
var openSetupWin = function(){
	var data = $('#pop_win .data').text();
	var m = eval('('+data+')');
	
	 var setting = {
		     check: {
		         enable: true
		     },
		     data: {
		         simpleData: {
		             enable: true
		         }
		     }
	 };
	
	 var json = common.ajaxRequest('service', {"p":"authorityMenuManage","m":"permissionList","roleId":m.id}, 'POST');
	 var zNodes = json.zNodes;
	 
	$.fn.zTree.init($("#permissionTree"), setting, zNodes);
	$('#pop_win').modal('hide');
	$('#permission .roleId').val( m.id );
	$('#permission').modal('show');
};

/**
 * 设置权限
 */
var setPermission = function(){
	var treeObj = $.fn.zTree.getZTreeObj("permissionTree");
	var nodes =  treeObj.transformToArray(treeObj.getNodes());
	var resourceIds = '';
	$(nodes).each(function(){
		var json = this.getCheckStatus();
		if( json.checked)
		{
			resourceIds += this.id +',';
		}
	});
	
	if( resourceIds!='' )
	{
		resourceIds = resourceIds.substring( 0 , resourceIds.lastIndexOf(',') );
	}	
	
	var roleId = $('#permission .roleId').val();
	var resultJson = common.ajaxRequest('service', {"p":"authorityMenuManage","m":"setPermission","resourceIds":resourceIds,"roleId":roleId} , 'POST');
	if( resultJson && resultJson.result == 0 )
	{
		$('#permission').modal('hide');
		common.showMsg('info', '', '设置成功!');
	}
	else
	{
		common.showMsg('error', '设置失败', resultJson.message);
		return;
	}	
	
};

/**
 * 打开编辑窗口
 * @returns
 */
var showModifyWin = function(obj){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	$('#editPop .id').val(json.id);
	$('#editPop .name').val(json.name);
	$('#pop_win').modal('hide');
	$('#editPop').modal('show');
};

/**
 * 新建资源
 * @returns
 */
var newRole = function(){

	var name = $('#newPop .name').val();
	var datas = {"p":"roleManage","m":"new","name":name };
	var json = common.ajaxRequest('service', datas , 'POST');
	if( json && json.result == 0 )
	{
		roleList();
		$('#newPop').modal('hide');
		common.showMsg('info', '', '新建成功!');
	}
	else
	{
		common.showMsg('error', '创建失败', json.message);
	}	
};

/**
 * 编辑分组
 * @returns
 */
var modifyRole = function(){
	
	var id = $('#editPop .id').val();
	var name = $('#editPop .name').val();
	var datas = {"p":"roleManage","m":"modify","name":name,"id":id };
	var json = common.ajaxRequest('service', datas , 'POST');
	if( json && json.result == 0 )
	{
		roleList();
		$('#editPop').modal('hide');
		common.showMsg('info', '', '修改成功');
	}
	else
	{
		common.showMsg('error', '修改成功', json.message);
	}		
};


